package com.wgt.startx.helper;

import com.wgt.startx.annotation.RequestMapping;
import com.wgt.startx.bean.Handler;
import com.wgt.startx.bean.Request;
import com.wgt.startx.util.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 控制器助手类
 * Created by wgt on 2017/7/30.
 */
public final class ControllerHelper {
    private static Map<Request,Handler> REQUESTMAPPING_MAP = new HashMap<Request, Handler>();
    private static Logger logger = LoggerFactory.getLogger(ControllerHelper.class);

    /**
     * 获取所有controller类
     */
    static{
        Set<Class<?>> classSet = ClassHelper.getControllerClassSet();
        if (CollectionUtils.isNotEmpty(classSet)){
            for (Class<?> cls:classSet){
                Method[] methods = cls.getMethods();
                if (CollectionUtils.isNotEmpty(methods)){
                    for (Method method :methods){
                        if (method.isAnnotationPresent(RequestMapping.class)){
                            RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
                            String mapping = requestMapping.value();
                            logger.info("request url mapping:{}",mapping);


                            // 判断 Request Path 中是否带有占位符
                            if (mapping.matches(".+\\{\\w+\\}.*")){
                                System.out.println("requestMapping coming");
                                String[] array = mapping.split(":");
                                if(CollectionUtils.isNotEmpty(array) && array.length==2){
                                    //获取请求方法和请求路径
                                    String requestMethod = array[0];
                                    String requestPath = array[1];
                                    Request request = new Request(requestMethod,requestPath);
                                    Handler handler = new Handler(cls,method);
                                    REQUESTMAPPING_MAP.put(request,handler);
                                }
                            }else{
                                Request request = new Request("get",mapping);
                                Handler handler = new Handler(cls,method);
                                REQUESTMAPPING_MAP.put(request,handler);
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 获取handler
     * @param requestMethod
     * @param requestPath
     * @return
     */
    public static Handler getHandler(String requestMethod,String requestPath){
        if (StringUtils.isEmpty(requestMethod)||StringUtils.isEmpty(requestPath)) throw new NullPointerException();
        Request request = new Request(requestMethod,requestPath);
        Handler handler = REQUESTMAPPING_MAP.get(request);
        return handler;
    }

}
